#coding=utf-8
import arcpy
path = r'D:\WorkData\20250712四川河流\0818处理'
arcpy.env.workspace = path
arcpy.env.overwriteOutput = True
spatial_ref = arcpy.SpatialReference(4326)
riv = r'D:\WorkData\20250712四川河流\0818处理\rivs1.shp'.decode('utf-8')
riv2 = r'D:\WorkData\20250712四川河流\0818处理\rivs2.shp'.decode('utf-8')

expression1 = "level = '0' "
cs1 = arcpy.da.UpdateCursor(riv,['SHAPE@','name','rivId','level','lpDisRiv','flowRiv','flowId'],where_clause=expression1)

cs2 = arcpy.da.SearchCursor(riv2,['SHAPE@','name','rivId','level'])
allRivs = [row for row in cs2]
i = 0
for row1 in cs1:
    disList = []
    for riv in allRivs:
        lp = arcpy.PointGeometry(row1[0].lastPoint,spatial_ref)
        dis = lp.distanceTo(riv[0])
        nr_name = riv[1]
        nr_id = riv[2]
        nr_lv = riv[3]
        res = [dis, nr_name, nr_id, nr_lv]
        if row1[2] != riv[2] and riv[3] != '0':
            disList.append(res)
    disList.sort(key=lambda x: x[0])
    print i,row1[1], '流入', disList[0][1], disList[0][0], disList[0][2], disList[0][3]
    row1[4] = disList[0][0]
    row1[5] = disList[0][1]
    row1[6] = disList[0][2]
    cs1.updateRow(row1)
    i += 1

